package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

public class TreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(5);

    /**
     * todo 线程的开启的方式
     * 1. 继承 Thread 类 ,重写run 方法  new Thread01();
     * <p>
     * 2.实现Runable  接口 ,重写run方法 new Thread(Thread02)
     * <p>
     * 3.实现Callable 接口,+FutureTask 并携带返回值 ,重写call 方法. Callable<T>  ,return T , 实现的还是Runable 接口.  new Thread(THread03);
     * <p>
     * 4.线程池 .  从线程池中开启线程  , 原理都事继承于ExecutorService 接口.
     * 4.1  ExecutorService  service=new Executors.newFIxedThreadPool(参数);  service.excute(new Thread());
     * 4.2  new ThreadPoolExecutor([7 大参数]);
     * * @param args
     *
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws Exception {
           /* CompletableFuture<Void> future= CompletableFuture.runAsync(()->{
                int i=10/2;
                System.out.println("当前线程的id:"+Thread.currentThread().getId()+"当前线程的结果="+i);
            },executor);*/
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            int i = 10 / 2;
            return i;
        }, executor);
       /* CompletableFuture<Integer> exceptionally = future.whenComplete((result, exception) -> {
            System.out.println("结果是:" + result + "异常是" + exception);
        }).exceptionally(throwable -> {
            return 0;
        });*/

     /*   CompletableFuture<Integer> handle = future.handle((res, exc) -> {

            return 0;
        });
        System.out.println(handle.get());*/
        //线程的串行
      /*  CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            return "hello"+Thread.currentThread().getId();
        }, Executors.newFixedThreadPool(5));
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            return "world"+Thread.currentThread().getId();
        }, Executors.newFixedThreadPool(5));
  *//*      future1.runAfterBothAsync(future2,()->{
            System.out.println("任务3开始执行"+Thread.currentThread().getId());
        },executor);*//*

        future1.thenAcceptBothAsync(future2,(f1,f2)->{

                System.out.println(f1+f2);

        },executor);*/
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("商品图片查询" + Thread.currentThread().getId());
            return "商品图片查询";
        });
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("商品属性查询" + Thread.currentThread().getId());
            return "商品属性查询";
        });
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("商品介绍查询" + Thread.currentThread().getId());
            return "商品介绍查询";
        });
        CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2, future3);
        allOf.get();

        System.out.println("程序结束");

    }

    /*    public  static class athrows ExecutionException, InterruptedException {
     *//*  Thread02 thread02 = new Thread02();
        new Thread(thread02).start();*//*

     *//* Thread01 thread01 = new Thread01();
        thread01.start();*//*

     *//*FutureTask<Integer> integerFutureTask = new FutureTask<>(new Thread03());
        Thread thread = new Thread(integerFutureTask);
        thread.start();
        Integer integer = integerFutureTask.get();
        System.out.println(integer);*//*

     *//* service.execute(new Thread02());*//*
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor();

    }*/
    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前的线程:" + Thread.currentThread().getId());
            int i = 10 / 3 + 10 % 3;
            System.out.println("运行的结果" + i);
        }
    }

    public static class Thread02 implements Runnable {

        @Override
        public void run() {
            System.out.println("当前的线程:" + Thread.currentThread().getId());
            int i = 10 / 3 + 10 % 3;
            System.out.println("运行的结果" + i);
        }
    }

    public static class Thread03 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            return (10 / 3 + 10 % 3);
        }
    }
}

